﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Simulacao.aspx.cs" Inherits="ViewLayer.Pages.MonteCarlo.Simulacao" %>

<%@ Register Assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI.DataVisualization.Charting" TagPrefix="asp" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    <script type="text/javascript" src="../../Scripts/DrawSimulationGraphic.js"></script>
    <%--<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>--%>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#<%= txtDataFase.ClientID %>").datepicker({
                dateFormat: 'dd/mm/yy',
                dayNames: ['Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sábado'],
                dayNamesMin: ['D', 'S', 'T', 'Q', 'Q', 'S', 'S', 'D'],
                dayNamesShort: ['Dom', 'Seg', 'Ter', 'Qua', 'Qui', 'Sex', 'Sáb', 'Dom'],
                monthNames: ['Janeiro', 'Fevereiro', 'Março', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro'],
                monthNamesShort: ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
                nextText: 'Próximo',
                prevText: 'Anterior'
            });
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>P&aacute;gina de simula&ccedil;&atilde;o de Monte Carlo -
        <asp:Label ID="lblNomeProjeto" CssClass="" runat="server" Text="Nome do Projeto"></asp:Label></h2>

    <hr />
    <br />
    <div runat="server" id="pre_chart_div"></div>
    <asp:DropDownList ID="ddlType" runat="server" OnSelectedIndexChanged="ddlType_SelectedIndexChanged" AutoPostBack="True">
        <asp:ListItem Text="Selecione..." Value="0"></asp:ListItem>
        <asp:ListItem Text="Epics" Value="1"></asp:ListItem>
        <asp:ListItem Text="Hist&oacute;ria" Value="2"></asp:ListItem>
        <asp:ListItem Text="Tarefa" Value="3"></asp:ListItem>
        <asp:ListItem Text="Release" Value="4"></asp:ListItem>
        <asp:ListItem Text="Sprint" Value="5"></asp:ListItem>
    </asp:DropDownList>
    <asp:Button ID="btnSelect" runat="server" Text="OK" OnClick="btnSelect_Click" />
    <br />
    <asp:Panel ID="pnlDataFase" runat="server" Visible="false">
        <asp:Label ID="lblDataFase" runat="server" Text="Selecione a Data, a partir da qual ir&aacute; se considerar as atividades: "></asp:Label>
        <asp:TextBox ID="txtDataFase" runat="server"></asp:TextBox>
    </asp:Panel>
    <br />
    <asp:Label ID="lblMsgError" runat="server" Text="Por favor, selecione um tipo!" ForeColor="Red" Visible="false"></asp:Label>
    <br />
    <br />
    <asp:GridView ID="grdFase" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333"
        GridLines="None" OnRowCommand="grdFase_RowCommand" OnRowDataBound="grdFase_RowDataBound" Visible="false">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="Nome">
                <ItemTemplate>
                    <asp:Label ID="lblNomeFase" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Data In&amp;iacute;o">
                <ItemTemplate>
                    <asp:Label ID="lblDataInicioFase" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Data T&amp;eacute;rmino">
                <ItemTemplate>
                    <asp:Label ID="lblDataTerminoFase" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Visualizar">
                <ItemTemplate>
                    <asp:Button ID="btnVisualizar" runat="server" CommandName="Visualizar" Text="Visualizar" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>
    <br />
    <asp:DropDownList ID="ddlPhaseType" runat="server" Visible="false">
    </asp:DropDownList>
    <asp:Label ID="lblMsgErroTipoFase" runat="server" Text="Por favor selecione o tipo que deseja visualizar!" ForeColor="Red" Visible="false"></asp:Label>
    <%--<asp:Button ID="btnSelectPhaseType" runat="server" Text="OK" OnClick="btnSelectPhaseType_Click" />--%>
    <br />
    <br />
    <br />

    <asp:Label ID="lblIterations" runat="server" Text="Escreva a quantidade de itera&ccedil;&otilde;es que deseja: "></asp:Label>
    <asp:TextBox ID="txtIterations" runat="server"></asp:TextBox>
    <asp:Button ID="btnSimulateJS" runat="server" Text="Simular" />
    <asp:Label ID="lblMsgErrorFilling" runat="server" Text="Por favor preencha o n&uacute.mero de itera&ccedil;&otilde;es!" ForeColor="Red" Visible="false"></asp:Label>
    <br />
    <br />

    <asp:GridView ID="grdResultado" runat="server" AutoGenerateColumns="False"
        CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDataBound="grdResultado_RowDataBound" OnRowCommand="grdResultado_RowCommand">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:TemplateField HeaderText="Selecione">
                <HeaderTemplate>
                    <input id="chkAll" onclick="javascript: SelectAllCheckboxes(this);" value="chkAll"
                        runat="server" type="checkbox" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="ckbSelect" runat="server" />
                    <asp:HiddenField ID="lblID" runat="server"></asp:HiddenField>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ID" Visible="false">
                <ItemTemplate>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Nome">
                <ItemTemplate>
                    <asp:Label ID="lblNome" runat="server" Text=""></asp:Label>
                    <asp:TextBox ID="txtNome" runat="server" Visible="false"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Melhor Caso">
                <ItemTemplate>
                    <asp:Label ID="lblMelhorCaso" runat="server" Text=""></asp:Label>
                    <asp:TextBox ID="txtMelhorCaso" runat="server" Visible="false"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Mais Prov&amp;aacute;vel">
                <ItemTemplate>
                    <asp:Label ID="lblMaisProvavel" runat="server" Text=""></asp:Label>
                    <asp:TextBox ID="txtMaisProvavel" runat="server" Visible="false"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Pior Caso">
                <ItemTemplate>
                    <asp:Label ID="lblPiorCaso" runat="server" Text=""></asp:Label>
                    <asp:TextBox ID="txtPiorCaso" runat="server" Visible="false"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Associado">
                <ItemTemplate>
                    <asp:Label ID="lblAssociado" runat="server" Text=""></asp:Label>
                    <asp:DropDownList ID="ddlToAssociate" runat="server" Visible="false">
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="">
                <ItemTemplate>
                    <asp:Button ID="btnAtualizar" runat="server" Text="Editar" CommandName="Editar" />
                    <asp:Button ID="btnSalvar" runat="server" Text="Salvar" CommandName="Salvar" Visible="false" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

    <div id="chart_div"></div>
    <br />
    <script type="text/javascript">
        function SelectAllCheckboxes(spanChk) {
            // Added as ASPX uses SPAN for checkbox
            var oItem = spanChk.children;
            var theBox = (spanChk.type == "checkbox") ?
                spanChk : spanChk.children.item[0];
            xState = theBox.checked;
            elm = theBox.form.elements;

            for (i = 0; i < elm.length; i++)
                if (elm[i].type == "checkbox" &&
                         elm[i].id != theBox.id) {
                    //elm[i].click();
                    if (elm[i].checked != xState)
                        elm[i].click();
                    //elm[i].checked=xState;
                }
        }
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            //debugger;
            $("#<%=btnSimulateJS.ClientID%>").click(function (evt) {
                //debugger;
                var final = "";
                $("#<%=grdResultado.ClientID%> input:checkbox:checked").each(function () {
                    if ($(this).val() != "chkAll") {

                        final += "," + $(this).next("input[name$=lblID]").val();
                    }
                });

                final = final.replace(",", "")

                var iterations = $("#<%=txtIterations.ClientID%>").val();
                var dataForAjax = "{'id':'" + final + "','iterations':'" + iterations + "'}";

                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    contentType: 'application/json',
                    url: 'Simulacao.aspx/GetData',
                    data: dataForAjax,
                    success:
                        function (response) {
                            drawVisualization(response.d);
                        }
                });

                evt.preventDefault();
            });
        });

        google.load('visualization', '1', { packages: ['corechart'] });

        function drawVisualization(dataValues) {
            var data = new google.visualization.DataTable();

            data.addColumn('number', 'Probability');
            data.addColumn('number', 'Probalidade');

            for (var i = 0; i < dataValues.length; i++) {
                data.addRow([dataValues[i].ColumnName, dataValues[i].Value]);
            }

            var options = {
                title: 'Estimativa', width: 1000, height: 380,
                vAxis: { title: 'Probalidade', textStyle: { color: '#005500', fontSize: '12', paddingRight: '100', marginRight: '100' } },
                hAxis: { title: 'Tempo', textStyle: { color: '#005500', fontSize: '12', paddingRight: '100', marginRight: '100' } }
            }

            new google.visualization.LineChart(document.getElementById('chart_div')).draw(data, options);
        }
    </script>
</asp:Content>
